package com.ahao.servlet.teacher;

import com.ahao.model.RespBean;
import com.ahao.model.RespPageBean;
import com.ahao.model.TeacherDTO;
import com.ahao.service.TeacherService;
import com.ahao.utils.CommonsUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/teacher")
public class TeacherServlet extends HttpServlet {

    private TeacherService teacherService = new TeacherService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if ("page".equals(action)) {
            req.getRequestDispatcher("/WEB-INF/jsp/teacher/teacherList.jsp").forward(req, resp);
        }else if ("data".equals(action)) {
            String page = req.getParameter("page");
            String rows = req.getParameter("rows");
            String sort = req.getParameter("sort");
            String order = req.getParameter("order");
            RespPageBean respPageBean = teacherService.getTeacherList(page, rows, sort, order);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(new ObjectMapper().writeValueAsString(respPageBean));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String number = req.getParameter("number");
        String name = req.getParameter("name");
        String gender = req.getParameter("gender");
        String phone = req.getParameter("phone");
        String qq = req.getParameter("qq");
        String[] courses = req.getParameterValues("course[]");

        resp.setContentType("application/json;charset=utf-8");
        RespBean respBean = null;
        Integer i = teacherService.addTeacher(number, name, gender, phone, qq, courses);
        if (i == CommonsUtils.UPDATE_SUCCESS){
            respBean = RespBean.ok("添加成功！");
        }else if (i == CommonsUtils.TABLE_DATA_REPEAT){
            respBean = RespBean.error("教职工号重复！");
        }else if (i == CommonsUtils.OTHER_EXCEPTION){
            respBean = RespBean.error("添加失败！");
        }else{
            respBean = RespBean.error("添加失败！");
        }
        resp.getWriter().write(new ObjectMapper().writeValueAsString(respBean));
    }

    /**
     * 在 post 请求中，请求体里边的参数会被自动转为 key-value 的形式，然后用户直接调用 getParameter 方法读取即可
     * 但是，在 put 请求中，请求体里边的参数不会被自动转化，所以我们有两种解决方案：
     * 1. 手动转化
     * 2. 用 json
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TeacherDTO teacherDTO = new ObjectMapper().readValue(req.getInputStream(), TeacherDTO.class);
        Integer i = teacherService.updateTeacher(teacherDTO);
        RespBean respBean = null;
        if (i == CommonsUtils.UPDATE_SUCCESS){
            respBean = RespBean.ok("修改成功！");
        }else if (i == CommonsUtils.OTHER_EXCEPTION){
            respBean = RespBean.error("修改失败！");
        }else {
            respBean = RespBean.error("修改失败！");
        }

        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(new ObjectMapper().writeValueAsString(respBean));
    }
}
